home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifcico / ftsc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-27  |  6.3 KB  |  334 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "ftn.h"
  4. #include "getheader.h"
  5. #include "xutil.h"
  6. #include "lutil.h"
  7. #include "session.h"
  8. #include "ttyio.h"
  9. #include "statetbl.h"
  10. #include "config.h"
  11.  
  12. extern int master;
  13. extern int nodelock(faddr*);
  14.  
  15. int rx_ftsc(void);
  16. int tx_ftsc(void);
  17.  
  18. extern unsigned long sequencer(void);
  19. extern int xmrecv(char*);
  20. extern int xmsend(char*,char*,int);
  21. extern int xmsndfiles(file_list*);
  22. extern int sendbark(void);
  23. extern int recvbark(void);
  24. extern void rdoptions(node*);
  25.  
  26. static int rxftsc(void);
  27. static int txftsc(void);
  28. static int recvfiles(void);
  29. static file_list *tosend;
  30.  
  31. int rx_ftsc(void)
  32. {
  33.     int rc;
  34.  
  35.     loginf("start inbound ftsc session");
  36.     session_flags |= SESSION_BARK;
  37.     rc=rxftsc();
  38.     if (rc)
  39.     {
  40.         loginf("inbound ftsc session failed, rc=%d",rc);
  41.         PUTCHAR(CAN);
  42.         PUTCHAR(CAN);
  43.         PUTCHAR(CAN);
  44.     }
  45.     else debug(10,"inbound ftsc session rc=%d",rc);
  46.     tidy_filelist(tosend,0);
  47.     tosend=NULL;
  48.     return rc;
  49. }
  50.  
  51. int tx_ftsc(void)
  52. {
  53.     int rc;
  54.  
  55.     loginf("start outbound ftsc session with %s",
  56.         ascfnode(remote->addr,0x1f));
  57.     rc=txftsc();
  58.     if (rc)
  59.     {
  60.         loginf("outbound ftsc session failed, rc=%d",rc);
  61.         PUTCHAR(CAN);
  62.         PUTCHAR(CAN);
  63.         PUTCHAR(CAN);
  64.     }
  65.     else debug(10,"outbound ftsc session rc=%d",rc);
  66.     tidy_filelist(tosend,0);
  67.     tosend=NULL;
  68.     return rc;
  69. }
  70.  
  71. SM_DECL(txftsc,"txftsc")
  72. SM_STATES
  73.     wait_command,recv_mail,send_req,recv_req
  74. SM_NAMES
  75.     "wait_command","recv_mail","send_req","recv_req"
  76. SM_EDECL
  77.  
  78.     int c,rc;
  79.     char *nonhold_mail;
  80.  
  81.     if (localoptions & NOHOLD) nonhold_mail=ALL_MAIL;
  82.     else nonhold_mail=NONHOLD_MAIL;
  83.     tosend=create_filelist(remote,nonhold_mail,2);
  84.  
  85.     if ((rc=xmsndfiles(tosend))) return rc;
  86.  
  87. SM_START(wait_command)
  88.  
  89. SM_STATE(wait_command)
  90.  
  91.     c=GETCHAR(30);
  92.     if (c == TIMEOUT)
  93.     {
  94.         loginf("timeout waiting for remote action, try receive");
  95.         SM_PROCEED(recv_mail);
  96.     }
  97.     else if (c < 0)
  98.     {
  99.         loginf("got error waiting for TSYNC: received %d",c);
  100.         SM_ERROR;
  101.     }
  102.     else switch (c)
  103.     {
  104.     case TSYNC:    SM_PROCEED(recv_mail);
  105.             break;
  106.     case SYN:    SM_PROCEED(recv_req);
  107.             break;
  108.     case ENQ:    SM_PROCEED(send_req);
  109.             break;
  110.     case 'C':
  111.     case NAK:    PUTCHAR(EOT);
  112.             SM_PROCEED(wait_command);
  113.             break;
  114.     case CAN:    SM_SUCCESS;  /* this is not in BT */
  115.             break;
  116.     default:    if (c > ' ') debug(10,"got '%c' waiting command",c);
  117.             else debug(10,"got '\\%03o' waiting command",c);
  118.             PUTCHAR(SUB);
  119.             SM_PROCEED(wait_command);
  120.             break;
  121.     }
  122.  
  123. SM_STATE(recv_mail)
  124.  
  125.     if (recvfiles()) {SM_ERROR;}
  126.     else {SM_PROCEED(wait_command);}
  127.  
  128. SM_STATE(send_req)
  129.  
  130.     if (sendbark()) {SM_ERROR;}
  131.     else {SM_SUCCESS;}
  132.  
  133. SM_STATE(recv_req)
  134.  
  135.     if (recvbark()) {SM_ERROR;}
  136.     else {SM_PROCEED(wait_command);}
  137.  
  138. SM_END
  139. SM_RETURN
  140.  
  141.  
  142. SM_DECL(rxftsc,"rxftsc")
  143. SM_STATES
  144.     recv_mail,send_mail,send_req,recv_req
  145. SM_NAMES
  146.     "recv_mail","send_mail","send_req","recv_req"
  147. SM_EDECL
  148.  
  149.     int c,count=0;
  150.  
  151. SM_START(recv_mail)
  152.  
  153. SM_STATE(recv_mail)
  154.  
  155.     if (recvfiles()) {SM_ERROR;}
  156.     else {SM_PROCEED(send_mail);}
  157.  
  158. SM_STATE(send_mail)
  159.  
  160.     if (count > 6) {SM_ERROR;}
  161.  
  162.     if (tosend == NULL) {SM_PROCEED(send_req);}
  163.  
  164.     PUTCHAR(TSYNC);
  165.     c=GETCHAR(15);
  166.     count++;
  167.     if (c == TIMEOUT)
  168.     {
  169.         loginf("no NAK, cannot send mail");
  170.         SM_ERROR;
  171.     }
  172.     else if (c < 0)
  173.     {
  174.         loginf("got error waiting for NAK: received %d",c);
  175.         SM_ERROR;
  176.     }
  177.     else switch (c)
  178.     {
  179.     case 'C':
  180.     case NAK:    if (xmsndfiles(tosend)) {SM_ERROR;}
  181.             else {SM_PROCEED(send_req);}
  182.             break;
  183.     case CAN:    loginf("remote refused to pick mail");
  184.             SM_SUCCESS;
  185.             break;
  186.     case EOT:    PUTCHAR(ACK);
  187.             SM_PROCEED(send_mail);
  188.             break;
  189.     default:    if (c > ' ') debug(10,"got '%c' waiting NAK",c);
  190.             else debug(10,"got '\\%03o' waiting NAK",c);
  191.             SM_PROCEED(send_mail);
  192.             break;
  193.     }
  194.  
  195. SM_STATE(send_req)
  196.  
  197.     if (count > 6) {SM_ERROR;}
  198.  
  199.     if ( 1 /* has no .req file */ ) {SM_PROCEED(recv_req);}
  200.  
  201.     PUTCHAR(SYN);
  202.     c=GETCHAR(15);
  203.     count++;
  204.     if (c == TIMEOUT)
  205.     {
  206.         loginf("no ENQ, cannot send requests");
  207.         SM_ERROR;
  208.     }
  209.     else if (c < 0)
  210.     {
  211.         loginf("got error waiting for ENQ: received %d",c);
  212.         SM_ERROR;
  213.     }
  214.     else switch (c)
  215.     {
  216.     case ENQ:    if (sendbark()) {SM_ERROR;}
  217.             else {SM_PROCEED(recv_req);}
  218.             break;
  219.     case CAN:    loginf("remote refused to accept request");
  220.             SM_PROCEED(recv_req);
  221.             break;
  222.     case 'C':
  223.     case NAK:    PUTCHAR(EOT);
  224.             SM_PROCEED(send_req);
  225.             break;
  226.     case SUB:    SM_PROCEED(send_req);
  227.             break;
  228.     default:    if (c > ' ') debug(10,"got '%c' waiting ENQ",c);
  229.             else debug(10,"got '\\%03o' waiting ENQ",c);
  230.             SM_PROCEED(send_req);
  231.             break;
  232.     }
  233.  
  234. SM_STATE(recv_req)
  235.  
  236.     if (recvbark()) {SM_ERROR;}
  237.     else {SM_SUCCESS;}
  238.  
  239. SM_END
  240. SM_RETURN
  241.  
  242.  
  243. SM_DECL(recvfiles,"recvfiles")
  244. SM_STATES
  245.     recv_packet,scan_packet,recv_file
  246. SM_NAMES
  247.     "recv_packet","scan_packet","recv_file"
  248. SM_EDECL
  249.  
  250.     int rc=0;
  251.     char recvpktname[16];
  252.     char *fpath;
  253.     FILE *fp;
  254.     faddr f,t;
  255.     fa_list **tmpl;
  256.  
  257. SM_START(recv_packet)
  258.  
  259. SM_STATE(recv_packet)
  260.  
  261.     sprintf(recvpktname,"%08lx.pkt",sequencer());
  262.     rc=xmrecv(recvpktname);
  263.     if (rc == 1) {SM_SUCCESS;}
  264.     else if (rc == 0)
  265.     {
  266.         if (master) {SM_PROCEED(recv_file);}
  267.         else {SM_PROCEED(scan_packet);}
  268.     }
  269.     else {SM_ERROR;}
  270.  
  271. SM_STATE(scan_packet)
  272.  
  273.     fpath=xstrcpy(inbound);
  274.     fpath=xstrcat(fpath,"/");
  275.     fpath=xstrcat(fpath,recvpktname);
  276.     fp=fopen(fpath,"r");
  277.     free(fpath);
  278.     if (fp == NULL)
  279.     {
  280.         logerr("$cannot open received packet");
  281.         SM_ERROR;
  282.     }
  283.     switch (getheader(&f,&t,fp))
  284.     {
  285.     case 3:    loginf("remote mistook us for %s",ascfnode(&t,0x1f));
  286.         fclose(fp);
  287.         SM_ERROR;
  288.     case 0:    loginf("accepting session");
  289.         fclose(fp);
  290.         for (tmpl=&remote;*tmpl;tmpl=&((*tmpl)->next));
  291.         (*tmpl)=(fa_list*)xmalloc(sizeof(fa_list));
  292.         (*tmpl)->next=NULL;
  293.         (*tmpl)->addr=(faddr*)xmalloc(sizeof(faddr));
  294.         (*tmpl)->addr->zone=f.zone;
  295.         (*tmpl)->addr->net=f.net;
  296.         (*tmpl)->addr->node=f.node;
  297.         (*tmpl)->addr->point=f.point;
  298.         (*tmpl)->addr->name=NULL;
  299.         (*tmpl)->addr->domain=NULL;
  300.         for (tmpl=&remote;*tmpl;tmpl=&((*tmpl)->next))
  301.             (void)nodelock((*tmpl)->addr);
  302.             /* try lock all remotes, ignore locking result */
  303.         if (((nlent=getnlent(remote->addr))) &&
  304.             (nlent->pflag != DUMMY))
  305.         {
  306.             loginf("remote is a listed system");
  307.             inbound=listinbound;
  308.             rdoptions(nlent);
  309.         }
  310.         if (f.name)
  311.         {
  312.             loginf("remote gave correct password, protected FTS-0001 session");
  313.             inbound=listinbound;
  314.         }
  315.         tosend=create_filelist(remote,ALL_MAIL,1);
  316.         if (rc == 0) {SM_PROCEED(recv_file);}
  317.         else {SM_SUCCESS;}
  318.     default: loginf("received bad packet apparently from",ascfnode(&f,0x1f));
  319.         fclose(fp);
  320.         SM_ERROR;
  321.     }
  322.  
  323. SM_STATE(recv_file)
  324.  
  325.     switch (xmrecv(NULL))
  326.     {
  327.     case 0:        SM_PROCEED(recv_file); break;
  328.     case 1:        SM_SUCCESS; break;
  329.     default:    SM_ERROR; break;
  330.     }
  331.  
  332. SM_END
  333. SM_RETURN
  334.